﻿(function ($) {
    $.fn.multiFileUploader = function(options) {
        var $this = $(this);

        var defaultOpt = {
            uploadUrl: "",
            accept: "",
            maxFileSize:-1,
            fieldName: "file",
            fileNameEdit: false, //是否选择文件后,弹出文件名修改的框
            sender: $this,
            afterUploadCallback:null,
            name: "",
            disabled:false
        }

        var tbody = $this.find("table tbody");

        var opt = $.extend({}, defaultOpt, options);

        var buildItem = function(data) {
            var tr = $("<tr></tr>");

            tr.append(
                "<td><i class=\"fileIcon fa fa-fw " + getMimeIcon(__getFileExtension(data.FileUrl), data.Mime) + "\"></i></td>");

            tr.append("<td class='filename' ><span>" + data.Title + "</span><input class=\"fileurl\" type=\"hidden\" name=\"" + opt.name + "\" value=\"" + data.FileUrl+"\"/></td>");

            tr.append(
                '<td  class="buttonlist"><span class="badge bg-info badge-view" style=\"cursor: pointer;display:none;margin-right=5px\">查看</span><span class="badge bg-red badge-del" style=\"cursor: pointer;\">删除</span></td>');

            tr.data("fileinfo-mime", data.Mime);
            tr.data("fileinfo-title", data.Title);


            return tr;
        }

        var refreshRowEvent = function(row) {
            var delBadge = $(row).find(".badge-del");
            var viewBadge = $(row).find(".badge-view");

            delBadge.click(function () {
                $(row).closest("tr").remove();
            });

            var json =row.data("fileinfo");

            viewBadge.click(function () {
                var url = $(row).find(".fileurl").val();

                WebUIJS.ShowFileViewer(url, { mime: json.Mime});
            });
        }

        var addNewItem = function(data) {
            var tr = buildItem(data);

            tbody.append(tr);

            refreshRowEvent(tr);

            $(tr).find(".badge-status").remove();
            $(tr).find(".badge-view").show();

            refreshInputFileInfo();

            return tr;
        }

        var init = function() {
            $this.find("table.filetable tbody tr").each(function (index, value) {
                var tr = $(this);

                refreshRowEvent(tr);
            });

            refreshInputFileInfo();
        }

        var refreshInputFileInfo = function () {
            var infos = [];

            $this.find("table tbody tr").each(function() {
                var mime = $(this).data("fileinfo-mime");
                var title = $(this).data("fileinfo-title");
                var url = $(this).find("input[type=hidden]").val();
                
                infos.push({
                    Mime: mime,
                    Title: title,
                    FileUrl:url
                });
            });

            $this.find(".allfileinfo").val(JSON.stringify(infos));
        }

        $this.find(".addfile").click(function () {

            if (opt.uploadUrl && opt.uploadUrl != "") {
                WebUIJS.ShowUploadFile(opt.uploadUrl,
                    {
                        accept: opt.accept,
                        fieldName: opt.fieldName,
                        maxFileSize: opt.maxFileSize,
                        onSuccess: function(ret, f) {
                            if (ret.IsSuccess) {
                                var data = {
                                    Title: f.name,
                                    Mime: f.type,
                                    FileUrl: ret.ReturnData
                                }

                                addNewItem(data);

                                WebUIJS.Toast("上传成功");

                                if (opt.afterUploadCallback) {
                                    __callBackFuncOrStr(opt.afterUploadCallback, opt.sender, [data]);
                                }

                            } else {

                                WebUIJS.Alert(data.Message);
                            }
                        },
                        onError: function() {
                            WebUIJS.Toast("上传失败");
                        },
                        onAfterSelectFile: function(file) {
                            if (opt.fileNameEdit) {
                                var fileName = confirm(f.name);

                                file.name = fileName;
                            }
                        }
                    });
            } else {
                var file = $("<input type=\"file\" style=\"display:none\" name=\"" + opt.fieldName + "\" accept=\"" + opt.accept + "\" />");

                $(file).change(function() {
                    if (this.files.length <= 0) {
                        $(this).remove();
                        return false;
                    }

                    if (opt.maxFileSize && opt.maxFileSize > 0 && this.files[0].size > opt.maxFileSize) {
                        $(this).remove();
                        WebUIJS.Alert("文件太大,不能上传");
                        return false;
                    }
                    
                    if (opt.fileNameEdit) {
                        WebUIJS.Confirm(this.files[0].name,
                            "新文件名",
                            function (txt) {
                                var data = {
                                    Title: txt,
                                    Mime: f.type,
                                    FileUrl:""
                                }

                                var tr = addNewItem(data);

                                tr.find(".filename").append(file);

                                if (opt.afterUploadCallback) {
                                    __callBackFuncOrStr(opt.afterUploadCallback, opt.sender, [data]);
                                }
                            });
                    } else {
                        var data = {
                            Title: this.files[0].name,
                            Mime: f.type,
                            FileUrl: ""
                        }

                        var tr = addNewItem(data);

                        tr.find(".filename").append(file);

                        if (opt.afterUploadCallback) {
                            __callBackFuncOrStr(opt.afterUploadCallback, opt.sender, [data]);
                        }
                    }


                });


            }
        });

        init();
    }

    

})(jQuery);